package com.jdew.thkj.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.activity.LoginActivity;
import com.jdew.thkj.activity.OrderMingXiActivity;
import com.jdew.thkj.activity.PddDetailActivity;
import com.jdew.thkj.adapter.PddOrderAdapter;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.bean.PddOrderBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.utils.DeviceUtils;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;
import com.jdew.thkj.utils.WeiboDialogUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PDDOrderFragment extends BaseFragment {
    @Bind(R.id.recyclerview)
    RecyclerView recycleView;
    @Bind(R.id.smartRefresh)
    SmartRefreshLayout smartRefreshLayout;
    @Bind(R.id.noresult)
    TextView noresult;
    @Bind(R.id.result_view)
    RelativeLayout resultView;
    @Bind(R.id.pddorder_view)
    LinearLayout pddview;
    private int page = 1;
    private PddOrderAdapter orderadapter;
    private String itemId;
    private List<PddOrderBean.DataBean> orderList = new ArrayList<>();
    private OrderMingXiActivity activity;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_pddorder;
    }

    @Override
    protected void onCreateViewT() {
        activity = (OrderMingXiActivity) getActivity();
        orderadapter = new PddOrderAdapter(activity, orderList);
        sp = activity.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        setRcycleview(recycleView, orderadapter);
        orderadapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (!RegexValidateUtils.isFastClick()) {
                    return;
                }
                if (sp.getString("SpareTwo", "").equals("")) {
                    startActivity(new Intent(activity, LoginActivity.class));
                } else if (sp.getString("UserSjPhone", "").equals("") && (!sp.getString("SpareTwo", "").equals("1"))) {
                    activity.toset("检测到您还没有绑定上级,请到我的页面绑定上级");
                } else {
                    itemId = orderList.get(position).getGoods_id();
                    toDetail(itemId);
                }
            }
        });
        smartRefreshLayout.setRefreshHeader(new MaterialHeader(activity));
        smartRefreshLayout.setRefreshFooter(new BallPulseFooter(activity).setSpinnerStyle(SpinnerStyle.Scale));
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                smartRefreshLayout.setEnableLoadMore(true);
                GetOrderDetail();
                smartRefreshLayout.finishRefresh();
            }

        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                GetOrderDetail();
                smartRefreshLayout.finishLoadMore();
            }
        });
    }

    @Override
    public void initData() {
        GetOrderDetail();
    }

    public void toDetail(String itemId) {
        Bundle b = new Bundle();
        b.putSerializable("itemId", itemId);
        startActivity(PddDetailActivity.class, b);
    }

    public void GetOrderDetail() {
        if (!DeviceUtils.isNetworkAvalible(activity)) {
            if (!activity.isFinishing()) {
                ToastUtils.getInstance(activity).showMessage("网络连接错误请检查");
            }
            return;
        }
        activity.mWeiboDialog = WeiboDialogUtils.createLoadingDialog(activity);
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).PDDGetOrder(sp.getString("userPhone", ""), page, RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "UserPhone" + sp.getString("userPhone", "") + "PageIndex" + page)).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<PddOrderBean>() {
            @Override
            public void onCompleted() {
                WeiboDialogUtils.closeDialog(activity.mWeiboDialog);
            }

            @Override
            public void onError(Throwable e) {
                WeiboDialogUtils.closeDialog(activity.mWeiboDialog);
            }

            @Override
            public void onNext(PddOrderBean bean) {
                WeiboDialogUtils.closeDialog(activity.mWeiboDialog);
                if (bean.getCode().equals("0")) {
                    if (bean.getNum().equals("0") && page == 1) {
                        pddview.setVisibility(View.GONE);
                        resultView.setVisibility(View.VISIBLE);
                        noresult.setText("您还没有订单哦");
                        return;
                    }
                    pddview.setVisibility(View.VISIBLE);
                    if (page == 1) {
                        orderList.clear();
                    }
                    orderList.addAll(bean.getData());
                    orderadapter.setDataList(orderList);
                    if (bean.getData().size() < 30 && bean.getData().size() > 0) {
                        smartRefreshLayout.setEnableLoadMore(false);
                    } else {
                        if (orderList.size() == 0 && page == 1) {
                            smartRefreshLayout.setVisibility(View.GONE);
                            resultView.setVisibility(View.VISIBLE);
                            noresult.setText("您还没有订单哦");
                            return;
                        }
                    }
                }
            }
        });
    }
}
